﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Command;
using RestaurantBooker.Model;
using GalaSoft.MvvmLight;
using RestaurantBooker.ServiceProcessor;
using System.ComponentModel;
using RestaurantBooker.View;

namespace RestaurantBooker.ViewModel
{
    public class LoginPageViewModel : ViewModelBase
    {
        public ServiceProcessor.ServiceProcessor ServiceProcessorInstance
        {
            get { return ServiceProcessor.ServiceProcessor.Instance; }
        }

        #region [ Commands ]
        public RelayCommand CloseCommand
        {
            get;
            private set;
        }


        public RelayCommand LoginCommand
        {
            get;
            private set;
        }

        private void InitializeCommands()
        {
            LoginCommand = new RelayCommand(() =>
            {
                ServiceProcessor.ServiceProcessor.Instance.LoginBooker(this);
            });

            CloseCommand = new RelayCommand(() =>
            {
                if (_childWindowContainer != null)
                    _childWindowContainer.Close();
            });
        }
        #endregion

        public LoginPageViewModel()
        {
            //ServiceProcessor.ServiceProcessor.Instance.UserCredentials = new Model.UserCredentials();

#if DEBUG
            ServiceProcessor.ServiceProcessor.Instance.UserCredentials.UserName = "booker1";
            //ServiceProcessor.ServiceProcessor.Instance.UserCredentials.Password = "booker1";
#endif
            InitializeCommands();

            if (!String.IsNullOrEmpty(ServiceProcessor.ServiceProcessor.Instance.UserCredentials.UserName)
                && !String.IsNullOrEmpty(ServiceProcessor.ServiceProcessor.Instance.UserCredentials.Password))
            {
                LoginCommand.Execute(null);
            }
        }

        /// <summary>
        /// The <see cref="ErrorMessage" /> property's name.
        /// </summary>
        public const string ErrorMessagePropertyName = "ErrorMessage";

        private String _errorMessage = "";

        /// <summary>
        /// Gets the ErrorMessage property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public String ErrorMessage
        {
            get
            {
                return _errorMessage;
            }

            set
            {
                if (_errorMessage == value)
                {
                    return;
                }

                _errorMessage = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ErrorMessagePropertyName);
            }
        }

        internal void LoginComplete(bool result)
        {
            //need to close login dialog and show some next work page

            if (result)
            {
                var booker = ServiceProcessor.ServiceProcessor.Instance.CurrentBooker;
                if (booker != null)
                {
                    if (booker.is_admin)
                        ViewModelLocator.MainStatic.OpenFrameByFrameType(Model.Enums.FrameType.AdminPage);
                    else
                        ViewModelLocator.MainStatic.OpenFrameByFrameType(Model.Enums.FrameType.BookerPage);
                }
            }
        }

        private ChildWindowContainer _childWindowContainer = null;

        public void Show()
        {
            if (_childWindowContainer == null)
            {
                _childWindowContainer = new View.ChildWindowContainer()
                {
                    Title = "Login",
                    Content = new LoginPage() { DataContext = this }
                };
                _childWindowContainer.Closing += new EventHandler<CancelEventArgs>(ChildWindowContainer_Closing);
            }

            _childWindowContainer.Show();
        }

        void ChildWindowContainer_Closing(object sender, CancelEventArgs e)
        {
           
        }

        #region [ ICleanup ]
        /// <summary>
        /// Clean up view model resources
        /// </summary>
        public override void Cleanup()
        {
            CloseDialog();
            base.Cleanup();
        }

        /// <summary>
        /// Forced dialog window clsoe in case connection is broken or Logout message received
        /// </summary>
        private void CloseDialog()
        {
            if (_childWindowContainer != null && _childWindowContainer.DialogIsOpened)
                _childWindowContainer.Close();
        }
        #endregion
    }
}
